package com.rjwh.dingdong.client.activity.more;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.more.fragment.MorePointsDetialFragment;
import com.rjwh.dingdong.client.fragment.BaseFragment;
import com.rjwh.dingdong.client.widget.PagerSlidingTabStrip;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 积分明细
 *
 * @author Kay
 * @ClassName MorePointsDetialActivity
 * @Description
 * @date 2016-9-23
 * @history 2016-9-23 author: description:
 */
public class MorePointsDetialActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.more_points_detial_total_points_tv)
    TextView morePointsDetialTotalPointsTv;
    @BindView(R.id.more_points_detial_pagelidingtab)
    PagerSlidingTabStrip morePointsDetialPagelidingtab;
    @BindView(R.id.more_points_detial_viewpager)
    ViewPager morePointsDetialViewpager;

    private ArrayList<BaseFragment> listFragment = new ArrayList<>();
    private String totalscore;

    public static void startActivity(Context context, String totalscore) {
        Intent intent = new Intent(context, MorePointsDetialActivity.class);
        intent.putExtra("totalscore", totalscore);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.more_points_detial_layout);
        ButterKnife.bind(this);
//        parseIntent();
        initView();
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            totalscore = intent.getStringExtra("totalscore");
        }
    }

    private void initView() {
        initToolbarHelper();
        MorePointsDetialFragment pointsDetialFragmentEarn = MorePointsDetialFragment.newInstance(1);//收入
        MorePointsDetialFragment pointsDetialFragmentPay = MorePointsDetialFragment.newInstance(2);//支出
        listFragment.add(pointsDetialFragmentEarn);
        listFragment.add(pointsDetialFragmentPay);
        MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
        morePointsDetialViewpager.setAdapter(adapter);
        morePointsDetialPagelidingtab.setViewPager(morePointsDetialViewpager);
        initTabsValue();
//        morePointsDetialTotalPointsTv.setText(totalscore);
    }

    private void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle(getResources().getString(R.string.more_points_detial));
        }
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    /* ***************FragmentPagerAdapter***************** */
    public class MyPagerAdapter extends FragmentPagerAdapter {
        private final String[] TITLES = {"收入", "支出"};

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return listFragment.size();
        }

        @Override
        public Fragment getItem(int position) {
            return listFragment.get(position);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return super.isViewFromObject(view, object);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // super.destroyItem(container, position, object);
        }
    }

    /**
     * mPagerSlidingTabStrip默认值配置
     */
    private void initTabsValue() {
        // 底部游标颜色@color/blue_bg
        morePointsDetialPagelidingtab.setIndicatorColor(getResources().getColor(R.color.blue_button_nor));
        morePointsDetialPagelidingtab.setTabPaddingLeftRight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // tab的分割线颜色
        morePointsDetialPagelidingtab.setDividerColor(Color.TRANSPARENT);
        // tab背景
        morePointsDetialPagelidingtab.setBackgroundColor(getResources().getColor(R.color.white));
        // tab底线高度
        morePointsDetialPagelidingtab.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 游标高度
        morePointsDetialPagelidingtab.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
        // 选中的文字颜色
        morePointsDetialPagelidingtab.setSelectedTextColor(getResources().getColor(R.color.blue_button_nor));
        // 正常文字颜色
        morePointsDetialPagelidingtab.setTextColor(Color.BLACK);
        morePointsDetialPagelidingtab.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        morePointsDetialPagelidingtab.setShouldExpand(true);
    }
}
